<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/08/10 0010
 * Time: 10:54:19
 */
namespace app\admin\Controller;
use app\common\controller\Curt;

class User extends Curt{
    public function getall()
    {
        $keyword=input('get.keyword');
        $where['status']=1;
        if($keyword){
            $where['user_realname|user_nickname']=['like','%'.$keyword.'%'];//模糊查询or可以一个条件中写多个字段以‘|’分开
        }
        $user=$this->model->where($where)->order('id desc')->paginate();
        $page = $user->render();
        $this->assign(
            ['user'=>$user],
            ['page'=>$page]
        );
    }
    public function adduser(){
        $data=$this->request->param();
        $validate=validate($this->table);
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }
        $data['password']=md5($data['password']);
        $data['user_avator']='avater.jpg';
        $data['addtime']=time();
        $flag=$this->model->save($data);
        if($flag){
            $this->success('添加成功',url('showlist'));
        }else{
            $this->error('操作失败',url('showlist'));
        }
    }
    /**
     * 查看详情
     */
    public function showdetails(){
        $where['id']=input('id');
        $user=$this->model->with('refreeid')->where($where)->find()->toArray();

        $this->assign(
            ['user'=>$user]
        );
       return $this->fetch();
    }

    /**
     * 重置密码
     * @return \think\response\Json
     */
    public function refresspwd(){
        $data['id']=input('id');
        $data['password']=md5('a123456');
        $this->model->isUpdate(true)->save($data);
        $datas['state']=1;
        $datas['msg']='密码重置成功';
        return  json($datas);
    }
    /**
     * @return \think\response\Json
     * 开启关闭状态
     */
    public function editstatus(){
        $where['id']=input('id');
        $status=input('user_status');
        if($status==1){
            $status=-1;
        }else{
            $status=1;
        }
        $where['user_status']=$status;
        $this->model->isUpdate(true)->save($where);
        if($status==-1){
            $res['msg']="用户已关闭！";
        }else{
            $res['msg']="用户已开启！";
        }
        return json($res);
    }
}